package com.zqs.compareface.server.local;

import com.zqs.compareface.utils.ConnectionLocal;
import com.zqs.compareface.utils.impl.CreateFFmpegFramegrabberUtil;
import com.zqs.compareface.utils.impl.LocalCheckUtil;
import com.zqs.compareface.utils.impl.threadpool.TheCustomDynamicThreadPoolUtil;
import com.zqs.compareface.utils.impl.threadpool.myLinkedBlockingQueue.ReWritedCapacityLinkedBlockingQueue;
import javafx.scene.image.Image;
import org.bytedeco.javacv.*;
import org.bytedeco.javacv.Frame;
import org.bytedeco.opencv.opencv_core.IplImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.objdetect.CascadeClassifier;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Program: face
 * @Description:
 * @Author: zqs
 * @Create： 2021-08-10-19:31
 */
public class LocalDetectionFace {
    public Mat mat;
    public static void main(String[] args) {
        LocalDetectionFace localDetectionFace = new LocalDetectionFace();
        localDetectionFace.pullAndScanFaceAndShow();
    }
    /**
     * 拉流扫描和展示
     */
    public void pullAndScanFaceAndShow(){
        //调用FFmpegFrameGrabber创建工具
        FFmpegFrameGrabber grabber = CreateFFmpegFramegrabberUtil.createFFmpegFrameGrabber();
        /**
         * 开一个窗口查看视频画面，根据需要开启
         */
        CanvasFrame canvasFrame = new CanvasFrame("摄像机");
        canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvasFrame.setAlwaysOnTop(true);
        //转换器
        OpenCVFrameConverter.ToOrgOpenCvCoreMat converter = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();
        int i = 0;
        //开始抽帧和调用检测方法
        int index = 0;
        while (grabber != null) {
            Frame frame = null;
            try {
                frame = grabber.grabImage();
            } catch (FFmpegFrameGrabber.Exception e) {
                e.printStackTrace();
            }
            if (frame != null){
                Mat mat = converter.convert(frame);
                LocalCheckUtil.getFace2(mat);
                canvasFrame.showImage(frame);
//                HighGui.imshow("实时人脸识别", LocalCheckUtil.getFace2(mat));
//                index=HighGui.waitKey(1);
            }
        }
        CreateFFmpegFramegrabberUtil.closeFFmpegFrameGrabber(grabber);
    }

    /**
     * 拉流并调用人脸检测工具
     */
    public void pullAndScanFace(){
        //调用FFmpegFrameGrabber创建工具
        FFmpegFrameGrabber grabber = CreateFFmpegFramegrabberUtil.createFFmpegFrameGrabber();
        //转换器
        OpenCVFrameConverter.ToOrgOpenCvCoreMat converter = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();
        //开始抽帧和调用检测方法
        int i = 0;
        Mat resMat = null;
        while (grabber != null) {
            Frame frame = null;
            try {
                frame = grabber.grabImage();
            } catch (FFmpegFrameGrabber.Exception e) {
                e.printStackTrace();
            }
            if (frame != null){
                Mat mat = converter.convert(frame);
                LocalCheckUtil.getFace2(mat);
            }
        }
        CreateFFmpegFramegrabberUtil.closeFFmpegFrameGrabber(grabber);
    }
    /**
     * 多线程
     */
    public void pullAndShowWithThreadPool() {
        //调用FFmpegFrameGrabber创建工具
        FFmpegFrameGrabber grabber = CreateFFmpegFramegrabberUtil.createFFmpegFrameGrabber();
        /**
         * 开一个窗口查看视频画面，根据需要开启
         */
        CanvasFrame canvasFrame = new CanvasFrame("摄像机");
        canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvasFrame.setAlwaysOnTop(true);
        //转换器
        OpenCVFrameConverter.ToOrgOpenCvCoreMat converter = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();
        int i = 0;
        int index = 0;
        while (grabber != null) {
            Frame frame = null;
            try {
                frame = grabber.grabImage();
            } catch (FFmpegFrameGrabber.Exception e) {
                e.printStackTrace();
            }
            if (frame != null){
                Mat mat = converter.convert(frame);
                LocalCheckUtil.getFace2(mat);
                canvasFrame.showImage(frame);
            }
        }
        CreateFFmpegFramegrabberUtil.closeFFmpegFrameGrabber(grabber);
    }
}
